local get_item = function(group, active, sel)
    if active then
        if sel then
            group = group .. 'Sel'
        else
            group = group .. 'Active'
        end
    else
        if not sel then
            group = group .. 'Inactive'
        end
    end

    return '%#' .. group .. '#'
end

local tabline = function()
    local s = ''
    for index = 1, vim.fn.tabpagenr('$') do
        local winnr = vim.fn.tabpagewinnr(index)
        local bufnr = vim.fn.tabpagebuflist(index)[winnr]
        local bufname = vim.fn.bufname(bufnr)
        if #bufname == 0 then
            bufname = '[No Name]'
        end
        local bufmodified = vim.fn.getbufvar(bufnr, "&mod")
        local modified = ''
        if bufmodified == 1 then
            modified = '[+]'
        end


        local active = (index == vim.fn.tabpagenr())
        local filename = vim.fn.fnamemodify(bufname, ':t')

        local tabline_items = {
            get_item('Tabline', active, true),
            ' ',
            index,
            '. ',
            filename,
            modified,
            ' ',
        }
        s = s .. table.concat(tabline_items)

    end
    return s .. '%T%#TabLineFill#%='
end

function _G.nvim_tabline()
    return tabline()
end
vim.o.tabline = "%!v:lua.nvim_tabline()"
